package zhongbo.java.air.flightpath.service.serviceImpl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.util.JsonUtils;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.flightpath.listener.RedisLogChanelPublisher;
import zhongbo.java.air.flightpath.mapper.FlightPathMapper;
import zhongbo.java.air.flightpath.model.FlightPath;
import zhongbo.java.air.flightpath.service.FlightPathService;

import java.util.Date;

@Service
public class FlightPathServiceImpl extends ServiceImpl<FlightPathMapper, FlightPath> implements FlightPathService {

    @Autowired
    private FlightPathMapper flightPathMapper;

    @Autowired
    private RedisLogChanelPublisher redisLogChanelPublisher;

    @Override
    public PageVo<FlightPath> pageQuery(Integer page, Integer pageSize
            , String icao, String startTime, String endTime
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<FlightPath> queryWrapper = new QueryWrapper<>();
        Page<FlightPath> flightPathPage = new Page<>(page, pageSize);

        if (!(icao == null || icao.isEmpty())) {
            queryWrapper.eq("icao_code", icao);
        }

        if (startTime != null) {
            queryWrapper.ge("`timestamp`", startTime);
        }

        if (endTime != null) {
            queryWrapper.le("`timestamp`", endTime);
        }


        if (oderByField == null || oderByField.isEmpty()) {
            queryWrapper.orderByAsc("timestamp");
        } else {
            if (orderByAsc) {
                queryWrapper.orderByAsc(oderByField);
            } else {
                queryWrapper.orderByDesc(oderByField);
            }
        }

        //指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        Page<FlightPath> flightPathPage1 = flightPathMapper.selectPage(flightPathPage, queryWrapper);
        PageVo<FlightPath> flightPathPageVo = new PageVo<>(flightPathPage1);
        redisLogChanelPublisher.broadcast("查询航迹数据: " + flightPathPageVo.getRecords().toString());
        System.out.println("sql语句：" + queryWrapper.getSqlSegment());
        return flightPathPageVo;
    }

    @Override
    public ResultVo saveAirCraft(FlightPath flightPath) {
        flightPath.setTimestamp(new Date());

        if (flightPath.getIcaoCode() == null || flightPath.getIcaoCode().isEmpty()) {
            throw new BusinessException("关联飞机的ICAO码不能为空");
        }

        if (flightPath.getLatitude() == null || flightPath.getLatitude().toString().isEmpty()) {
            throw new BusinessException("纬度不能为空");
        }

        if (flightPath.getLongitude() == null || flightPath.getLongitude().toString().isEmpty()) {
            throw new BusinessException("经度不能为空");
        }

        if (flightPath.getAltitude() == null || flightPath.getAltitude().toString().isEmpty()) {
            throw new BusinessException("海报高度不能为空");
        } else if (flightPath.getAltitude().toString().length() > 11) {
            throw new BusinessException("海拔高度长度不能高于11位");
        }

        if (flightPath.getSourceType() == null || flightPath.getSourceType().isEmpty()) {
            throw new BusinessException("数据类型不能为空");
        }

//        if (!(flightPathMapper.selectAirCraftCode(flightPath.getIcaoCode()) > 0)){
//            throw new BusinessException("未查询到相关飞机的ICAO码");
//        }

        if (flightPathMapper.insert(flightPath) < 0) {
            return ResultVo.error("添加失败");
        }

        String json = JsonUtils.toJson(flightPath);

        /**
         * 发送到自己的广播
         */
        redisLogChanelPublisher.toMyBroadcast(json);
        System.out.println("发送的信息" + json);

        return ResultVo.ok("添加成功!");
    }

    @Override
    public ResultVo updateAirCraft(FlightPath flightPath) {
        flightPath.setTimestamp(new Date());
        if (flightPath.getId() == null || flightPath.getId().isEmpty()) {
            throw new BusinessException("id不能为null");
        }


        if (!(flightPathMapper.selectAirCraftCode(flightPath.getIcaoCode()) > 0)) {
            throw new BusinessException("未查询到相关飞机的ICAO码");
        }


        if (!(flightPathMapper.updateById(flightPath) > 0)) {
            return ResultVo.error("修改失败,无法锁定到对应轨迹");
        }

        redisLogChanelPublisher.broadcast("修改id为: " + flightPath.getId() + "的航迹数据: " + flightPath);
        return ResultVo.ok("修改成功!");
    }
}
